/*
 * ------------------------------------------------------------------
 * json-web-token 模块
 * ------------------------------------------------------------------
 * @关于我：
 * QQ:2815473064
 * wechat:13531609457
 * ------------------------------------------------------------------
 */
const JWT = require("jsonwebtoken");
const Config = require("../config");
// 密钥配置对象
const SecretKeyConfig = Config.SecretKey;
const DayJs = require("dayjs");
/**
 * 生成密钥
 * @param {*} token
 * @returns
 */
const createToken = (data = {}) => {
  let { way } = SecretKeyConfig;

  let secretkey = null;

  if (way == "token") {
    //   token配置对象
    let tokenConfig = SecretKeyConfig.type.token;
    let { secretOrPublicKey} = tokenConfig;
    secretkey = JWT.sign(data, secretOrPublicKey);
    return {
      secretkey,
      token: secretkey,
    };
  } else if (way == "cookie") {
  } else {
  }
};
/**
 * 验证token
 * @param {*} token
 * @returns
 */
function verify(secretkey) {
  let { way } = SecretKeyConfig;
  if (way == "token") {
    //   token配置对象
    let tokenConfig = SecretKeyConfig.type.token;
    let { secretOrPublicKey } = tokenConfig;
    return JWT.verify(secretkey, secretOrPublicKey, (err, decoded) => {
      if (err) {
        return {
          token: secretkey,
          state: 0,
        };
      } else {
        return {
          token: secretkey,
          state: 1,
          data: decoded,
        };
      }
    });
  } else if (way == "cookie") {
  } else {
  }
}
module.exports = {
  createToken,
  verify,
};
